import AlipaySdk from 'alipay-sdk';
import { appid, private_key, sign_type } from './config';

interface IToken {
	accessToken: string;
	alipayUserId: string;
	expiresIn: number;
	reExpiresIn: number;
	refreshToken: string;
	userId: string;
}
export default async function get_token(auth_code: string) {
	return new Promise<IToken>(async (resolve, _reject) => {
		const ali = new AlipaySdk({
			appId: appid,
			privateKey: private_key,
			signType: sign_type
		});

		const res = await ali.exec('alipay.system.oauth.token', {
			grant_type: 'authorization_code',
			code: auth_code
		}) as any as IToken;
		resolve(res)
	});
}
